package com.sajhasite.service;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sajhasite.authentication.UserContext;
import com.sajhasite.dao.IUserDao;
import com.sajhasite.domain.entity.User;

@Service
@Transactional
public class UserService implements IUserService {
	
	@Inject
	private IUserDao userDao;
	
	public IUserDao getUserDao() {
    	return userDao;
    }
	
	public void setUserDao(IUserDao userDao) {
    	this.userDao = userDao;
    }

	
	@Override
	public void createUser(String email, String password) {
		User user = new User();
		user.setEmail(email);
		//user.setUserName(userName);
		//if no display name, set email as display name by default
		user.setDisplayName(email);
		//
		user.setPassword(password);
		this.userDao.save(user);
	}
	
	@Override
	public void createUser(String email,String password,String displayName){
		User user = new User();
		user.setEmail(email);
		user.setDisplayName(displayName);
		user.setPassword(password);
		this.userDao.save(user);
	}
	

	
	@Override
	public UserContext authenticateUser(User user){
		User userFromDb=this.userDao.findUserByEmail(user.getEmail());
		UserContext userContext=new UserContext();
		if(userFromDb==null)
			return userContext;
		if(userFromDb.getPassword().trim().equals(user.getPassword().trim()))	{
			userContext.setLogged(Boolean.TRUE);
			userContext.setUser(userFromDb);
			return userContext;	
		}
			
		
		return userContext;
	}
	@Override
	public User getUser(String userName) {
		return this.userDao.findUserByEmail(userName);
	}
	
	

}
